
#include "RemoveOpera.h"

RemoveOpera::RemoveOpera(Target* target)
{
	_target = new Target(*target);
}

Target* RemoveOpera::target()
{
	return _target;
}

bool RemoveOpera::redo()
{
	if (!_target)
		return false;
	for (int i = 0; i < _target->pickHitCount(); i++)
	{
		BPickHit* hit = _target->pickHit(i);
		if (BShape* shape = hit->shape())
		{
			if (BNode* node = hit->node())
				node->removeShape(shape);
			else if (BGroup* group = hit->group())
				group->remove(shape);
			else if (BScene* scene = hit->scene())
				scene->remove(shape);
		}
		else if (BNode* node = hit->node())
		{
			if (BGroup* group = hit->group())
				group->remove(node);
			else if (BScene* scene = hit->scene())
				scene->remove(node);
		}
		else if (BGroup* group = hit->group())
		{
			if (BGroup* parent = group->parent())
				parent->remove(group);
			else if (BScene* scene = hit->scene())
				scene->remove(group);
		}
	}
	this->emit("target-changed");
	return true;
}
bool RemoveOpera::undo()
{
	if (!_target)
		return false;
	for (int i = 0; i < _target->pickHitCount(); i++)
	{
		BPickHit* hit = _target->pickHit(i);
		if (BShape* shape = hit->shape())
		{
			if (BNode* node = hit->node())
				node->insertShape(shape);
			else if (BGroup* group = hit->group())
				group->insert(shape);
			else if (BScene* scene = hit->scene())
				scene->insert(shape);
			return true;
		}
		else if (BNode* node = hit->node())
		{
			if (BGroup* group = hit->group())
				group->insert(node);
			else if (BScene* scene = hit->scene())
				scene->insert(node);
			return true;
		}
		else if (BGroup* group = hit->group())
		{
			if (BGroup* parent = group->parent())
				parent->insert(group);
			else if (BScene* scene = hit->scene())
				scene->insert(group);
		}
	}
	this->emit("target-changed", _target);
	return true;
}
